# Maintainer: Jeroen Ooms <jeroen@berkeley.edu>

_realname=tiledb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.13.0
pkgrel=1
pkgdesc="Storage management library for sparse and dense array data (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang32' 'clang64' 'clangarm64')
url="https://tiledb.com/"
license=("MIT")
depends=($([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp")
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-spdlog"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-file")
makedepends=("${MINGW_PACKAGE_PREFIX}-catch"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' '!debug' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/TileDB-Inc/TileDB/archive/${pkgver}.tar.gz"
        "0001-spdlog-external-fmt-library.patch"
        "0002-do-not-install-static-deps.patch")
sha256sums=('bc5aabc8bb2951a3882b660aa694353de7c22c5ba0a7ab12817452f3f6d2311d'
            'e1eff5b67e32f6cc2c74353ed619381f4b8a57bc794b5c7351dda993b41463e9'
            'e0e11a295b7e95dc61a974be685533eae29b09136727ef735264e212a3feea8f')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  tar -xf ${srcdir}/${_realname}-${pkgver}.tar.gz | true

  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-spdlog-external-fmt-library.patch"
  patch -Np1 -i "${srcdir}/0002-do-not-install-static-deps.patch"

  tar -xjf tiledb/sm/misc/magic_mgc_gzipped.bin.tar.bz2 -C tiledb/sm/misc
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _extra_config+=("-DTILEDB_S3=OFF")
  else
    _extra_config+=("-DTILEDB_S3=ON")
    CXXFLAGS+=" -Wno-c++11-narrowing"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DTILEDB_STATIC=ON \
    -DTILEDB_WERROR=OFF \
    -DTILEDB_SUPERBUILD=OFF \
    -DTILEDB_TESTS=OFF \
    -DTILEDB_LIBMAGIC_EP_BUILT=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # Remove full path reference in cmake files
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for cmfile in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/TileDB/*.cmake; do
    sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${cmfile}"
  done

  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
